package com.example.winner.gridview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("网络视图");
        GridView gv=(GridView)findViewById(R.id.grid);
        gv.setAdapter(new ImageAdapter(this));
    }
    public class ImageAdapter extends BaseAdapter
    {
        private Context context;
        public ImageAdapter(Context c)
        {
            context=c;
        }
        private Integer[] picture={};
        public int getCount()
        {
            return picture.length;
        }
        public Object getItem(int arg0)
        {
            return null;
        }
        @Override
        public long getItemId(int arg0)
        {
            return 0;
        }
        @Override
        public View getView(int position,View view,ViewGroup parent)
        {
            ImageView imageView;
            if(view==null)
            {
                imageView=new ImageView(context);
                imageView.setLayoutParams(new GridView.LayoutParams(85,85));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8,8,8,8);
            }
            else
            {
                imageView=(ImageView)view;
            }
            imageView.setImageResource(picture[position]);
            return imageView;
        }
    }
}
